package com.feng.chat.mapper;

import com.feng.chat.entity.TalkRecords;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户聊天记录表 Mapper 接口
 * </p>
 *
 * @author dyf
 * @since 2022-05-31
 */
public interface TalkRecordsMapper extends BaseMapper<TalkRecords> {

    /**
     * 获取私信对话的最后一条信息
     *
     * @param uid
     * @param rid
     * @return
     */
    @Select("SELECT * FROM talk_records WHERE talk_type=#{talkType} AND msg_type !=7 AND user_id IN(#{uid},#{rid}) AND receiver_id in (#{uid},#{rid}) AND is_revoke=0 AND id NOT IN(SELECT record_id FROM talk_records_delete WHERE user_id=#{uid})  ORDER BY updated_at DESC LIMIT 1")
    public TalkRecords getLastMessage(Integer talkType, Integer uid, Integer rid);

    /**
     * 获取群聊消息的最后一条信息
     *
     * @param talkType
     * @param group_id
     * @return
     */
    @Select("SELECT * FROM talk_records WHERE talk_type=#{talkType} AND msg_type !=7 AND receiver_id = #{group_id} AND is_revoke=0 AND id NOT IN(SELECT record_id FROM talk_records_delete WHERE user_id=#{uid})  ORDER BY updated_at DESC LIMIT 1")
    public TalkRecords getGroupLastMessage(Integer uid, Integer talkType, Integer group_id);

    /**
     * 获取聊天消息
     *
     * @param receiver_id
     * @param talk_type
     * @param limit
     * @param userId
     * @return
     */
    @Select({
            "<script>" +
                    "SELECT * FROM talk_records WHERE talk_type=#{talk_type} <if test='deletedAt !=null'> AND created_at &lt;= #{deletedAt}</if>  AND msg_type !=7 AND user_id IN(#{userId},#{receiver_id}) AND receiver_id in (#{userId},#{receiver_id}) <if test='record_id !=0'> AND id &lt; #{record_id}</if> AND id NOT IN(SELECT record_id FROM talk_records_delete WHERE user_id=#{userId}) ORDER BY created_at DESC LIMIT #{limit}" +
                    "</script>"
    })
    public List<TalkRecords> getRecordsList(Integer receiver_id, Integer record_id, Integer talk_type, Integer limit, Integer userId, String deletedAt);

    /**
     * 获取群组聊天消息
     *
     * @param group_id
     * @param record_id
     * @param talk_type
     * @param limit
     * @param userId
     * @return
     */
    @Select({
            "<script>" +
                    "SELECT * FROM talk_records WHERE talk_type=#{talk_type} AND msg_type !=7 AND receiver_id = #{group_id} <if test='record_id !=0'> AND id &lt; #{record_id}</if> <if test='deletedAt !=null'> AND created_at &lt;= #{deletedAt}</if> AND id NOT IN(SELECT record_id FROM talk_records_delete WHERE user_id=#{userId})  ORDER BY created_at DESC LIMIT #{limit}" +
                    "</script>"
    })
    public List<TalkRecords> getGroupRecordsList(Integer group_id, Integer record_id, Integer talk_type, Integer limit, Integer userId, String deletedAt);

    /**
     * 获取聊天历史记录
     *
     * @param userId
     * @param talk_type
     * @param receiver_id
     * @param record_id
     * @param msg_type
     * @param limit
     * @return
     */
    @Select({
            "<script>" +
                    "SELECT * FROM talk_records WHERE talk_type=#{talk_type}  AND msg_type NOT IN (0,7,9) <if test='msg_type != 0'> AND msg_type = #{msg_type} </if> <if test='talk_type==1'> AND user_id IN(#{userId},#{receiver_id}) AND receiver_id in (#{userId},#{receiver_id}) </if> <if test='talk_type==2'> AND receiver_id= #{receiver_id}</if>  <if test='record_id !=0'> AND id &lt; #{record_id}</if> AND id NOT IN(SELECT record_id FROM talk_records_delete WHERE user_id=#{userId}) ORDER BY created_at ASC LIMIT #{limit}" +
                    "</script>"
    })
    public List<TalkRecords> recordsHistory(Integer userId, Integer talk_type, Integer receiver_id, Integer record_id, Integer msg_type, Integer limit);

}
